﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System.Collections.Generic;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using ExtremeML.Spreadsheet;
using ExtremeML.Spreadsheet.Address;

namespace ExtremeML.Packaging
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class WorksheetPartWrapper
	{
		#region Custom Properties

		/// <summary>
		/// Gets the containing package as a <see cref="SpreadsheetDocumentWrapper"/>.
		/// </summary>
		public SpreadsheetDocumentWrapper Spreadsheet
		{
			get { return (SpreadsheetDocumentWrapper)Package; }
		}

		/// <summary>
		/// Gets the containing <see cref="WorkbookPartWrapper"/>.
		/// </summary>
		public WorkbookPartWrapper WorkbookPart
		{
			get { return ((SpreadsheetDocumentWrapper)Package).WorkbookPart; }
		}

		/// <summary>
		/// Gets the <see cref="SheetWrapper"/> that contains the workbook
		/// reference to the contained <see cref="WorksheetPart"/>.
		/// </summary>
		internal SheetWrapper Sheet
		{
			get { return WorkbookPart.Workbook.Sheets.Sheets.Find(Id); }
		}

		/// <summary>
		/// Gets or sets the name of the <see cref="Sheet"/> associated with the wrapped <see cref="WorksheetPart"/>.
		/// </summary>
		public string Name
		{
			get { return Sheet.Name; }
			set { Sheet.Name = value; }
		}

		/// <summary>
		/// Gets the <see cref="List{IAddressDependency}"/> that provides a central
		/// notification mechanism for wrapper classes affected by worksheet resizing.
		/// </summary>
		/// <remarks>
		/// Any wrapper class that manages document content that is anchored to
		/// worksheet cell addresses or cell address ranges should implement the
		/// <see cref="IAddressDependency"/> interface and be registered in this
		/// list in order to receive notifications when worksheet dimensions change
		/// through the insertion or deletion of rows or columns. Although wrapper
		/// classes may implement the interface directly, it is usually simpler for
		/// them to contain an instance of <see cref="CellAddress"/> or
		/// <see cref="CellArea"/> and to register that instance for notification.
		/// </remarks>
		internal List<IAddressDependency> AddressDependencies { get; private set; }

		#endregion
		#region Custom Methods

		/// <summary>
		/// Returns comments Vml drawing part for this worksheet, creating it as necessary.
		/// </summary>
		internal VmlDrawingPartWrapper GetCommentDrawingPart()
		{
			var result = VmlDrawingParts.Count == 0 ? VmlDrawingParts.Add() : VmlDrawingParts[0];
			Worksheet.LegacyDrawing.Id = result.Id;
			return result;
		}

		/// <summary>
		/// Safely terminates relationships with other package content objects.
		/// </summary>
		protected override void OnCustomDeactivating()
		{
			if (VmlDrawingParts.Count > 0)
			{
				var drawing = VmlDrawingParts[0];

				if (!drawing.Drawing.IsPopulated())
				{
					drawing.Remove();
					Worksheet.LegacyDrawing.Remove();
				}
			}
		}

		#endregion
		#region Overrides

		/// <summary>
		/// Implements custom initialization functionality.
		/// </summary>
		protected override void OnCustomInitializing()
		{
			AddressDependencies = new List<IAddressDependency>();
		}

		/// <summary>
		/// Implements custom population functionality.
		/// </summary>
		protected override void OnCustomDecomposing()
		{
			AddressDependencies = new List<IAddressDependency>();
		}
	
		#endregion
		#region Implementation Methods


		#endregion
		#region Static Methods

		/// <summary>
		/// Returns a <see cref="WorksheetPart"/> populated with custom default content.
		/// </summary>
		/// <param name="container">The parent <see cref="OpenXmlPartContainer"/> instance.</param>
        /// <param name="args">A parameter array of <see cref="object"/> containing member initialization arguments.</param>
        /// <returns>A new instance of <see cref="WorksheetPart"/>.</returns>
		internal static new WorksheetPart CreateCustomDefaultPart(
			OpenXmlPartContainer container,
            params object[] args)
		{
			var part = container.AddNewPart<WorksheetPart>();

			new Worksheet(
				new SheetDimension { Reference = "A1" },
				new SheetViews(
					new SheetView { WorkbookViewId = (UInt32Value)0U }),
				new SheetFormatProperties { DefaultRowHeight = 15D },
				new SheetData(),
				new PageMargins { Left = 0.7D, Right = 0.7D, Top = 0.75D, Bottom = 0.75D, Header = 0.3D, Footer = 0.3D }
			).Save(part);

			return part;
		}

		#endregion
	}
}
